package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.ProfTitleDao;
import cn.edu.sdjzu.xg.bysj.domain.ProfTitle;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class ProfTitleService {
    private static ProfTitleDao profTitleDao = ProfTitleDao.getInstance();
    private static ProfTitleService profTitleService = new ProfTitleService();


    public static ProfTitleService getInstance() {
        return profTitleService;
    }

    public Collection<ProfTitle> findAll
            (List<Condition> conditionList_json_str, Pagination pagination)
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<ProfTitle> profTitles = ProfTitleDao.findAll(connection, conditionList_json_str, pagination);
        JdbcHelper.close(connection);
        return profTitles;
    }

    public ProfTitle find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        ProfTitle result = profTitleDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(ProfTitle profTitle) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为ture
        connection.setAutoCommit(false);
        try {
            updated = profTitleDao.update(profTitle, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(ProfTitle profTitle) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = profTitleDao.add(profTitle, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        ProfTitle profTitle = this.find(id);
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = profTitleDao.delete(profTitle, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(ProfTitle profTitle_json, Connection connection) throws SQLException, BysjException {
        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "teacher", "profTitle_id", profTitle_json.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = profTitleDao.delete(profTitle_json, connection);
        } else {
            throw new BysjException(profTitle_json.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }

    public static class BysjException extends Exception {
        public BysjException(String message) {
            super(message);
        }
    }

}
